# -*- coding: utf-8 -*-
"""
@Time ： 2025/3/14 20:41
@Auth ： 九问
@File ：forms.py
@IDE ：PyCharm
@Email : 2750826557@qq.com
"""
from django import forms
from .models import Post, Category, Tag


class PostForm(forms.ModelForm):
    # 覆盖外键字段使用更友好的选择控件
    category = forms.ModelChoiceField(
        queryset=Category.objects.all(),
        widget=forms.Select(attrs={'class': 'select is-fullwidth'}),
        label='分类'
    )

    tags = forms.ModelChoiceField(
        queryset=Tag.objects.all(),
        widget=forms.Select(attrs={'class': 'select is-fullwidth'}),
        label='标签',
        required=False
    )

    class Meta:
        model = Post
        fields = ['title', 'desc', 'category', 'tags', 'content', 'is_hot']
        widgets = {
            'title': forms.TextInput(attrs={'class': 'input'}),
            'desc': forms.Textarea(attrs={'class': 'textarea', 'rows': 3}),
            'content': forms.Textarea(attrs={'class': 'textarea', 'rows': 10}),
            'is_hot': forms.CheckboxInput(attrs={'class': 'checkbox'}),
        }
        labels = {
            'is_hot': '设为热门文章'
        }


class CategoryForm(forms.ModelForm):
    class Meta:
        model = Category
        fields = ['name', 'desc']
        widgets = {
            'name': forms.TextInput(attrs={'class': 'input'}),
            'desc': forms.Textarea(attrs={'class': 'textarea', 'rows': 3}),
        }


class TagForm(forms.ModelForm):
    class Meta:
        model = Tag
        fields = ['name']
        widgets = {
            'name': forms.TextInput(attrs={'class': 'input'}),
        }
